* FILE LAST RUN ON 02/06/2024 USING STATA/SE 17.0

clear all
set maxvar 25000
set matsize 11000



cd ""
* Please change the directoray according to your set-up

/* TO RUN THIS FILE
1) Please download in a subfolder called BES the BES data for Stata from: https://www.britishelectionstudy.com/data-object/british-election-study-combined-wave-1-21-internet-panel-open-ended-response-data/
2) Unzip and have the BES2019_W25_Panel_v25.2.dta (at the time of writing, this the name of the file) in the BES subfolder.
*/



* First make it in panel data form
use .\BES\BES2019_W25_Panel_v25.2.dta, clear

keep id  blackEqualityW* femaleEqualityW* gayEqualityW* discrim* womenJob* lifeSat* lifeHappy* lifeWorthwhile* ///
gender ageW* p_educationW* p_ethnicityW* p_gross_householdW* p_housingW* p_maritalW* p_work_statW* p_job_sectorW* 

foreach var in blackEqualityW femaleEqualityW gayEqualityW {
	rename `var'1_W5 `var'5
	rename `var'6_W14 `var'14
}

duplicates drop id, force

save .\BES\BES2023_firstclean.dta, replace

* id should be unique so repeat must be mistake
use .\BES\BES2023_firstclean.dta, clear

reshape long  blackEqualityW femaleEqualityW gayEqualityW discrimMenW discrimWomenW discrimWhiteW discrimBMEW lifeSatW lifeHappyW lifeWorthwhileW womenJobsW ///
ageW p_educationW p_ethnicityW p_gross_householdW p_housingW p_maritalW p_work_statW p_job_sectorW  ///
, i(id) j(W) 

drop discrimChristiansW* discrimMuslimsW*

save .\BES\BES_long.dta, replace

* We want to recover the year of interview and map it to our MSOA variables (measured in June)

	use .\BES\BES_long.dta, clear

	generate blackTooMuch = 0 if blackEqualityW!=.
	replace blackTooMuch = 1 if blackEqualityW == 4 | blackEqualityW == 5
	* 1 if gone too far or much too far

	generate genderTooMuch = 0 if femaleEqualityW!=.
	replace genderTooMuch = 1 if femaleEqualityW == 4 | femaleEqualityW == 5
	* 1 if gone too far or much too far

	generate gayTooMuch = 0 if gayEqualityW!=.
	replace gayTooMuch = 1 if gayEqualityW == 4 | gayEqualityW== 5
	* 1 if gone too far or much too far

	gen womenadv = 0
	replace womenadv = . if womenJobsW == 9999 | womenJobsW == .
	replace womenadv = 1 if womenJobsW == 4 | womenJobsW == 5


	generate HigherEdu = 0 if p_educationW!=.
	replace HigherEdu = 1 if p_educationW >=15 & p_educationW<=18

	label var HigherEdu "University diploma"

	generate HighSchool = 0 if p_educationW!=.
	replace HighSchool = 1 if p_educationW<15 & p_educationW >1
	* If high school diploma or some professional qualification
	* reference category is no qualification or don't know/don't say
	label var HighSchool "High School and professional diploma"


	generate WhiteBrit = 0 if p_ethnicityW!=.
	replace WhiteBrit=1 if p_ethnicityW==1
	* White british



	generate OtherWhite = 0 if p_ethnicityW!=.
	replace OtherWhite=1 if p_ethnicityW==2

	gen White = max(WhiteBrit,OtherWhite)
	label var White "White"
	
	generate Male = 2-gender
	* Male is dummy equal to 1/ Female is zero

	label var Male "Male"
	
	gen WhiteMale = White * Male
	label var WhiteMale "White Male"

	

	generate Own = 0 if p_housingW!=.
	replace Own = 1 if p_housingW <4
	* Own with or without mortgage or shared ownership (very few)
	label var Own "Own house"

	generate Married = 0 if p_maritalW!=.
	replace Married = 1 if p_maritalW<3
	* Include civil partnership
	label var Married "Married"


	generate Divorced = 0 if p_maritalW!=.
	replace Divorced = 1 if p_maritalW == 3 | p_maritalW == 7
	* Include separated
	* Reference category all other arrangements
	label var Divorced "Divorced"


	
	forvalues j=1(1)15 {
	gen income`j' = 0 if p_gross_householdW !=.
	replace income`j'=1 if p_gross_householdW == `j'
	}
	gen income16 = 0 if p_gross_householdW !=.
	replace income16 = 1 if p_gross_householdW == 16 | p_gross_householdW == 17
	* Reference category is don't know, prefer not to say

	label var income1 "Income under £5000 per year"
	forvalues j=2(1)10 {
		local bottom=5000*`j'
		local top =5000*(`j'+1)-1
	label var income`j' "Income between £`bottom' and £`top' per year"
	}
	forvalues j=1(1)2{
		local bottom=50000 + 10000*(`j'-1)
		local top =50000 + 10000*(`j') - 1
		local k = `j'+10
	label var income`k' "Income between £`bottom' and £`top' per year"
	}
	label var income13 "Income between £70000 and £99999 per year"
	label var income14 "Income between £100000 and £149999 per year"
	label var income15 "Income over £150000 per year"

	forvalues j=1(1)8 {
		gen workstatus`j'=0 if p_work_statW!=.
		replace workstatus`j'=1 if p_work_statW == `j'
	}
	label var workstatus1 "Working full time ($\geq$ 30 h. per week)"
	label var workstatus2 "Working part time (8-29 h. per week)"
	label var workstatus3 "Working part time ($<$ 8 h. per week)"
	label var workstatus4 "Full time student"
	label var workstatus5 "Retired"
	label var workstatus6 "Unemployed"
	label var workstatus7 "Not working"
	* Reference category is Other

	forvalues j=1(1)3 {
		gen sector`j' = 0 if p_job_sectorW!=.
		replace sector`j'=1 if p_job_sectorW == `j'
	}
	gen sector4 = 0 if p_job_sectorW!=.
	replace sector4 = 1 if p_job_sectorW == 98  | p_job_sectorW == 99
	label var sector1 "Private sector"
	label var sector2 "Public sector"
	label var sector3 "Non-profit, non-government"
	* Reference category is don't know/not applicable

	label var ageW "Age"

	keep lifeHappyW lifeWorthwhileW blackTooMuch genderTooMuch gayTooMuch discrimMenW discrimWomenW discrimWhiteW  discrimBMEW ///
	WhiteMale White Male Own Married Divorced HigherEdu HighSchool ageW income1-income15 workstatus1-workstatus7 sector1-sector3 W
	
	order lifeHappyW lifeWorthwhileW blackTooMuch genderTooMuch gayTooMuch discrimMenW discrimWomenW discrimWhiteW  discrimBMEW ///
	WhiteMale White Male Own Married Divorced HigherEdu HighSchool ageW income1-income15 workstatus1-workstatus7 sector1-sector3 W
	 
	
	save .\BES\BES_final.dta, replace
